package com.queclink.ycm.entity.data.useless;

import com.alibaba.fastjson.JSONObject;
import com.queclink.ycm.common.utils.HexUtils;
import com.queclink.ycm.entity.data.Data;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-27 13:56:00
 */
public class Data262 extends Data {

    private String DTCsVersion;
    private String Number;
    private String DTCs;

    public String getDTCsVersion(JSONObject jsonObject) {
        StringBuffer sb = new StringBuffer();
        Integer dtCsVersion = jsonObject.getInteger("DTCsVersion");
        String s = HexUtils.intToHex(dtCsVersion, 2);
        sb.append(s);
        return sb.toString();
    }

    public String getNumber(JSONObject jsonObject) {
        StringBuffer sb = new StringBuffer();
        Integer number = jsonObject.getInteger("Number");
        String s = HexUtils.intToHex(number, 2);
        sb.append(s);
        return sb.toString();
    }

    public String getDTCs(JSONObject jsonObject) {
        StringBuffer sb = new StringBuffer();
        List<Map<String, Integer>> dtCs = (List<Map<String, Integer>>) jsonObject.get("DTCs");
        for (Map<String, Integer> dtC : dtCs) {
            Integer pcbuFaultCode = Integer.valueOf(dtC.get("PCBUFaultCode").toString());
            String pcbuFaultCodeStr = HexUtils.intToHex(pcbuFaultCode, 4);
            sb.append(pcbuFaultCodeStr);
            Integer faultCodeStatus = Integer.valueOf(dtC.get("FaultCodeStatus").toString());
            String faultCodeStatusStr = HexUtils.intToHex(faultCodeStatus, 4);
            sb.append(faultCodeStatusStr);
        }
        return DTCs;
    }

    public String getDataID() {
        // data262
        return "8106";
    }


    @Override
    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();
//        sb.append(this.getLength());
        sb.append(this.getDTCsVersion(jsonObject));
        sb.append(this.getNumber(jsonObject));
        sb.append(this.getDTCs(jsonObject));
        int len = sb.length();
        len = len / 2;
        if (len > 127) {
            sb.insert(0, HexUtils.intToHex(len, 4));
        } else {
            sb.insert(0, HexUtils.intToHex(len, 2));
        }
        sb.insert(0, this.getDataID());
        return sb.toString();
    }
}
